# Archlive <http://archlive-pkg.googlecode.com>


####################### Custom options#################
#### Class 1
std_config="config.i686"
sourcedir="$startdir"

#std_config="a8tc.config"
#sourcedir="/mnt/sda5"
#  如果预用自己电脑最keep得配置，则将内核配置文件放到跟PKGBUILD同目录下，文件名为i686.config
#  此时，其它一切特别设置都将无效

kver_file=kernel26.kver
preset_file=kernel26.preset	# used for "mkinitcpio -p kernel26"
kernel_name=vmlinuz26		# kernel file name
default_img=kernel26.img
fallback_img=kernel26-fallback.img
install_file=kernel26.install
#  如果要同一版本的不同次编译的内核共存  可以修改以上值
#  默认跟Arch官方 core testing中包一致

_configure_as_ARCH_way=y	# The "EXTRAVERSION" will be removed if you select "y" here. 2.6.31.5 as 2.6.31
_use_git_version=n		# Use the date as the version, otherwise use the standard Arch style, such as "2.6.33"
_re_menu_config=n			# re-configure before compile or not

#### Class 2
# Change the standard configuration base on the config.i686 or config.x86_64

local_version="-ARCH"		# Kernel appended version
_include_aufs2=y			# If aufs2 filesystem be included
_include_squashfs=y		# If squashfs4 filesystem be included
_use_bfs=n				# Use BFS scheduler or CFS scheduler

### Other Custom
_keep_build_dir=y			#If keep the kernel source directory
_save_config=y			#If save kernel configuration
_configure_only=n			#If just configure kernel, don't compile

######################################################
[ "${install_file}" != "kernel26.install" ] && cp -f $startdir/kernel26.install $startdir/${install_file}


pkgname=('kernel26' 'kernel26-firmware' 'kernel26-headers' 'linux-api-headers')
pkgbase=""
pkgver=$(date +%Y%m%d)
stdver=2.6.33
pkgrel=1
_realpkgname=kernel26
_kernelname=${_realpkgname#kernel26}
## if you want your own version, such as 2.6.31-archlive, then 
## "_configure_as_ARCH_way" set to "1" and
## "_realpkgname" change to "kernel26-archlive"
url="http://www.kernel.org/"
arch=(i686 x86_64)
license=('GPL2')
makedepends=('git' 'gcc')
backup=(etc/mkinitcpio.d/kernel26.preset)
source=(config.{i686,x86_64}  scsi.patch
	#Youbest 大侠的内核UTF-8中文字符补丁 http://blog.chinaunix.net/u/13265/showart.php?id=1008020
	http://archlive-pkg.googlecode.com/hg/kernel-utf-patches/common/utf8-kernel-2.6-fonts-2.patch.bz2
	http://archlive-pkg.googlecode.com/hg/kernel-utf-patches/2.6.32/utf8-kernel-2.6.31-fbcondecor-1.patch.bz2
	http://archlive-pkg.googlecode.com/hg/kernel-utf-patches/2.6.32/utf8-kernel-2.6.32-core-1.patch.bz2
)

_gitroot="http://git.zen-kernel.org/kernel/zen.git"
_gitname="zen.git"
_builddir="$srcdir/$_gitname-build"

build()
{
	KARCH="x86"

	#######################
	# Parepare Souces  #
	#######################
	msg "Fetching sources..."

	cd "$sourcedir"
#	if [ -d "$_gitname" ]; then
#		msg2 "Updating sources..."
#		cd "$_gitname" && git fetch && cd "$OLDPWD" #|| return 1
#	else
#		msg2 "Cloning the project (mirror)..."
#		git clone --mirror "$_gitroot" "$_gitname" || return 1
#	fi

	msg2 "Clone the kernel sources..."
	rm -rf "${_builddir}"
	git clone "$sourcedir"/"$_gitname" "${_builddir}" || return 1

	cd "${_builddir}"
	rm -rf .git

	#################
	# Apply patches
	#################
	cd "${_builddir}"

	msg2 "Apply utf-support patches..."
	patch -Np1 -i ${srcdir}/utf8-kernel-2.6.32-core-1.patch || return 1
	patch -Np1 -i ${srcdir}/utf8-kernel-2.6.31-fbcondecor-1.patch || return 1
	patch -Np1 -i ${srcdir}/utf8-kernel-2.6-fonts-2.patch || return 1

	msg2 "Apply patch to fix issues with transition of scsi.h from glibc"
	patch -Np1 -i ${srcdir}/scsi.patch || return 1

	# Apply other patches
	if [ -d "$startdir/patches" ]; then
		for i in "$startdir/patches/"*; do
			msg2 "Applying ${i##*/}..."
			patch -Np1 -i "$i" || (error "Applying ${i##*/} failed" && return 1)
		done
	fi

	#################
	# CONFIGURATION
	#################
	msg "Start kernel configuration"

	if [ "$_configure_as_ARCH_way" = "y" ]; then
		msg2 "Configure the kernel version as ARCH way, remove the extraversion"
		[ -f localversion[^~]* ] && rm -f localversion[^~]*
		sed -i "s|^EXTRAVERSION = .*$|EXTRAVERSION =|g" Makefile
	fi

	if [ -f $startdir/${std_config} ]; then
		# 如果有PKGBUILD同级目录下有.config 则采用该内核配置文件，不做任何其他配置
		msg2 "Use your specificed configuration file $startdir/${std_config}..."
		cp -f $startdir/${std_config} "${_builddir}"/.config
	else
		msg2 "Loading standard configuration..."
		for i in local "saved.$CARCH" "$CARCH"; do
			if [ -e "$startdir/config.$i" ]; then
				msg2 "Using kernel config file config.$i..."
				cp -f "$startdir/config.$i" .config || return 1
				break
			fi
		done

		if [ "${_use_bfs}" = "y" ]; then
			sed -i "s|.*CONFIG_CPU_CFS.*$|\# CONFIG_CPU_CFS is not set\
				\nCONFIG_CPU_BFS=y\
				\nCONFIG_CPU_BFS_AUTOISO=y|g" .config
		else
			sed -i "s|.*CONFIG_CPU_BFS.*$|\
				\nCONFIG_CPU_CFS=y\
				\n# CONFIG_CPU_BFS is not set|g" .config
		fi

		sed -i "s|.*CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${local_version}\"|g; \
			s|.*CONFIG_LOCALVERSION_AUTO.*|\# CONFIG_LOCALVERSION_AUTO is not set|g" .config

		if [ "$_include_squashfs" = "y" ]; then
			msg2 "Selected squashfs4 included in kernel package as module."
			if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" = "x" ]; then
				sed -i "s|.*CONFIG_SQUASHFS.*|CONFIG_SQUASHFS=m\
					\n# CONFIG_SQUASHFS_EMBEDDED is not set\
					\nCONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE=3|g" ./.config
			fi
		else
			if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" != "x" ]; then
				sed -i "s|CONFIG_SQUASHFS=m|\# CONFIG_SQUASHFS is not set|g" .config
			fi
		fi
		if [ "${_include_aufs2}" = "y" ]; then 
			msg2 "Selected aufs2 included in kernel package."
			#if [ "x$(grep "CONFIG_AUFS_FS=" .config)" = "x" ]; then
			sed -i "/.*CONFIG_NILFS2_FS.*/ a\
				\CONFIG_AUFS_FS=m\
				\n# CONFIG_AUFS_BRANCH_MAX_127 is not set\
				\n# CONFIG_AUFS_BRANCH_MAX_511 is not set\
				\nCONFIG_AUFS_BRANCH_MAX_1023=y\
				\n# CONFIG_AUFS_BRANCH_MAX_32767 is not set\
				\nCONFIG_AUFS_HINOTIFY=y\
				\nCONFIG_AUFS_EXPORT=y\
				\nCONFIG_AUFS_RDU=y\
				\nCONFIG_AUFS_SHWH=y\
				\nCONFIG_AUFS_BR_RAMFS=y\
				\nCONFIG_AUFS_BR_FUSE=y\
				\n# CONFIG_AUFS_DEBUG is not set\
				\nCONFIG_AUFS_BDEV_LOOP=y\
				\nCONFIG_AUFS_POLL=y" ./.config
			#fi
		else
			if [ "x$(grep "^CONFIG_AUFS_FS" .config)" != "x" ]; then
				sed -i "s|CONFIG_AUFS_FS.*|\# CONFIG_AUFS_FS is not set|g" .config
			fi
		fi
	
		# Change Kconfig so that aufs2 can be built as kernel module
		sed -i "s|	bool \"Aufs (Advanced multi layered unification filesystem) support\"|\
			tristate \"Aufs (Advanced multi layered unification filesystem) support\"|g" \
			fs/aufs/Kconfig
	fi

	if [ "${_re_menu_config}" = "y" ]; then
		make menuconfig
	fi
	yes "" | make config > /dev/null

	msg "Update .config Finished"

	###########################################
	###	Checking kernel version		##
	###########################################
	msg "Checking the kernel version..."
	make prepare
	_kernver="$(make kernelrelease)"

	if [ "$_configure_as_ARCH_way" = "y" ]; then
		# Check the _kernver again, if it is your wanted (Special for _configure_as_ARCH_way)
		_localversion=$(grep CONFIG_LOCALVERSION= .config | cut -d "\"" -f 2)
		if [ "${_kernver#*-}" != ${_localversion#-} ]; then
			msg2 "There are some errors when get your own kernel version."
			msg2 "_kernver=${_kernver}, _localversion=${_localversion}."
			exit 1
		else
			msg2 "Get the right kernel version as ${_kernver}"
		fi
	else
		msg2 "Get the kernel version as ${_kernver}"
	fi

	# Save the config file to the package directory
	if [ "${_save_config}" = "y" ] || [ "${_configure_only}" = "y" ]; then
		msg "Saving configuration..."
		msg2 "Saving "${_builddir}"/.config as $startdir/config.saved.$CARCH"
		cp .config "$startdir/config.saved.$CARCH" || return 1
	fi

	# Stop after configuration if desired
	if [ "${_configure_only}" = "y" ] && [ "${_keep_build_dir}" != "y" ]; then
		rm -rf "${_builddir}"
		return 1
	fi

	###########################################
	###	Compile				##
	###########################################

	msg "Building kernel and modules..."
	make bzImage modules || return 1
	msg "Compile finished!"

}

package_kernel26()
{
	pkgdesc="The Linux Kernel and modules from zen-kernel' git tree with BFS patches, Gentoo patches, aufs2 patch, and kernel Chinese characters patches."
	provides=("kernel26=$stdver" "kernel26=${stdver}${local_version}" 'kernel26-git')
	groups=('base')
	conflicts=('kernel24' 'kernel24-scsi' 'kernel26-scsi' 'unionfs')
	depends=('coreutils' 'module-init-tools' 'mkinitcpio>=0.5.15')
	install=${install_file}

	KARCH="x86"
	cd "${_builddir}"

	_kernver="$(make kernelrelease)"

	msg "Installing kernel image..."
	install -D -m644 System.map                "$pkgdir/boot/System.map26" &&
	install -D -m644 arch/$KARCH/boot/bzImage "$pkgdir/boot/${kernel_name}" || return 1
	#install -D -m644 .config                   "$pkgdir/boot/kconfig26"   

	#  # add vmlinux  官方加上的
	# 觉得多此一举
	#install -m644 -D vmlinux ${pkgdir}/usr/src/linux-${_kernver}/vmlinux 

	mkdir -p $pkgdir/etc/mkinitcpio.d

	msg "Installing kernel modules..."
	make INSTALL_MOD_PATH="$pkgdir" modules_install

	msg "Generating kernel26.kver..."
	echo -e "# DO NOT EDIT THIS FILE\nALL_kver='$_kernver'" \
		> "$pkgdir/etc/mkinitcpio.d/${kver_file}" || return 1

	msg "Generating preset file..."
	echo -e "# mkinitcpio preset file for $pkgname\n \
		\n########################################\
		\n# DO NOT EDIT THIS LINE:\
		\nsource /etc/mkinitcpio.d/${kver_file}\
		\n########################################\
		\nALL_config=\"/etc/mkinitcpio.conf\"\
		\n\nPRESETS=('default' 'fallback')\
		\n\n#default_config=\"/etc/mkinitcpio.conf\" \
		\ndefault_image=\"/boot/${default_img}\" \
		\n#default_options=\"\" \
		\n#fallback_config=\"/etc/mkinitcpio.conf\" \
		\nfallback_image=\"/boot/${fallback_img}\" \
		\nfallback_options=\"-S autodetect\" "\
		> "$pkgdir/etc/mkinitcpio.d/${preset_file}" || return 1

	msg "Update install scripts..."
	sed -i "s%pkgname=.*%pkgname=${pkgname}%g;\
		s%kernelname=.*%kernelname=${kernel_name}%g;\
		s%default_img=.*%default_img=${default_img}%g;\
		s%fallback_img=.*%fallback_img=${fallback_img}%g;\
		s%preset_file=.*%preset_file=${preset_file}%g"\
		$startdir/${install_file} || return 1

	msg "Remove the firmware..."
	rm -rf "$pkgdir/lib/firmware"

	msg "Kernel and modules installing finished."

	if [ "${_use_git_version}" != "y" ]; then
		pkgver=$stdver
	fi
}

package_kernel26-firmware()
{
	pkgdesc="The included firmware files of the Linux Kernel, from Linus' git tree."
	provides=("kernel26-firmware" "kernel26-firmware=$stdver" "kernel26-firmware-git")
	groups=('base')
	conflicts=('kernel26-firmware')

	cd "${_builddir}"

	msg "Install the kernel26-firmware..."
	make INSTALL_MOD_PATH="$pkgdir" firmware_install || return 1

	msg "kernel firmware installing finished."

	if [ "${_use_git_version}" != "y" ]; then
		pkgver=$stdver
	fi
}

package_kernel26-headers()
{
	pkgdesc="Kernel source from git tree, patched with aufs2 reiser4."
	provides=("kernel26-headers" "kernel26-headers=$stdver")
	groups=('base')
	conflicts=()

	KARCH="x86"
	cd "${_builddir}"
	_kernver="$(make kernelrelease)"

	msg "Installing the kernel include files..."

	# Install configuration and other files...
	install -D -m644 Makefile        "$pkgdir/usr/src/linux-$_kernver/Makefile"        &&
	install -D -m644 kernel/Makefile "$pkgdir/usr/src/linux-$_kernver/kernel/Makefile" &&
	install -D -m644 .config         "$pkgdir/usr/src/linux-$_kernver/.config" || return 1

	# kernel headers
	msg2 "base kernel headers..."
	[ ! -d $pkgdir/usr/src/linux-$_kernver/include ] && mkdir -p $pkgdir/usr/src/linux-$_kernver/include
	for i in acpi asm-generic asm-x86 drm config linux math-emu media net pcmcia scsi sound trace video; do
		if [ -e include/$i ]; then
			cp -af include/$i "$pkgdir/usr/src/linux-$_kernver/include/" || return 1
		fi
	done

	# Only latest git tree need below action
	cp -f include/generated/* "$pkgdir/usr/src/linux-$_kernver/include/linux/" #|| return 1

	msg2 "arch includes for external modules"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/x86/kernel
	cp -a arch/x86/include ${pkgdir}/usr/src/linux-${_kernver}/arch/x86/

	msg2 "necessary files for later builds, like nvidia and vmware"
	cp Module.symvers ${pkgdir}/usr/src/linux-${_kernver}

	msg2 "Install scripts directory and fix permissions on it"
	cp -a scripts "$pkgdir/usr/src/linux-$_kernver" &&
		chmod og-w -R "$pkgdir/usr/src/linux-$_kernver/scripts" || return 1

	msg2 "Makefile"
	cp arch/$KARCH/Makefile ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	if [ "$CARCH" = "i686" ]; then
		cp arch/$KARCH/Makefile_32.cpu ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/Makefile_32.cpu
	fi

	msg "Creating important symlinks..."
	cd "$pkgdir/usr/src/linux-$_kernver/include"
	ln -s ../arch/x86/include/asm asm
	#ln -s asm-generic asm
	cd "${_builddir}"

	mkdir -p $pkgdir/lib/modules/$_kernver
	cd "$pkgdir/lib/modules/$_kernver" &&
		rm -rf source build &&
		ln -s ../../../usr/src/linux-$_kernver build &&
		ln -s ../../../usr/src/linux-$_kernver source &&
		cd "$OLDPWD" || return 1

	msg "Installing architecture dependent files..."
	cp arch/$KARCH/kernel/asm-offsets.s ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel/asm-offsets.s

	# lirc headers
	msg2 "lirc"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/media/video" &&
		cp drivers/media/video/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/" || return 1

	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvideo zc0301; do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/$i" &&
			cp -a drivers/media/video/$i/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/media/video/$i" || return 1
	done

	# add docbook makefile
	install -D -m644 Documentation/DocBook/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Documentation/DocBook/Makefile

	# md headers
	msg2 "md"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/drivers/md" &&
		cp drivers/md/*.h "$pkgdir/usr/src/linux-$_kernver/drivers/md" || return 1

	# inotify.h
	msg2 "inotify.h"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/include/linux" &&
		cp include/linux/inotify.h "$pkgdir/usr/src/linux-$_kernver/include/linux/" || return 1

	# CLUSTERIP file for iptables
	msg2 "CLUSTERIP file for iptables"local_version
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/net/ipv4/netfilter/" &&
		cp net/ipv4/netfilter/ipt_CLUSTERIP.c "$pkgdir/usr/src/linux-$_kernver/net/ipv4/netfilter/"

	# wireless headers
	msg2 "wireless"
	mkdir -p "$pkgdir/usr/src/linux-$_kernver/net/mac80211/" &&
		cp net/mac80211/*.h "$pkgdir/usr/src/linux-$_kernver/net/mac80211/" || return 1

	# add dvb headers for external modules
	# in reference to: http://bugs.archlinux.org/task/9912
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core
	cp drivers/media/dvb/dvb-core/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core/
	# in reference to: http://bugs.archlinux.org/task/11194
	if [ -d include/config/dvb ]; then
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
		cp include/config/dvb/*.h ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
	fi
	# add dvb headers for http://mcentral.de/hg/~mrec/em28xx-new
	# in reference to: http://bugs.archlinux.org/task/13146
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/dvb/frontends/lgdt330x.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/video/msp3400-driver.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/

	# add xfs and shmem for aufs building
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/mm
	cp fs/xfs/xfs_sb.h ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs/xfs_sb.h

	msg2 "add headers for broadcom wl"
	# in reference to: http://bugs.archlinux.org/task/14568
	cp -a include/trace $pkgdir/usr/src/linux-${_kernver}/include/

	msg2 "add header for aufs2-util"
	#mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/asm &&
	cp -f include/asm-generic/bitsperlong.h ${pkgdir}/usr/src/linux-${_kernver}/include/asm/bitsperlong.h

	# Kconfig files
	msg2 "Kconfig files"
	for i in $(find . -name "Kconfig*"); do
		mkdir -p "$pkgdir/usr/src/linux-$_kernver/${i%/*}" &&
			cp "$i" "$pkgdir/usr/src/linux-$_kernver/$i" || return 1
	done

	cp arch/$KARCH/Makefile* "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH"
	cp -r arch/$KARCH/configs "$pkgdir/usr/src/linux-$_kernver/arch/$KARCH"

	# create a necessary symlink to the arch folder
	cd "$pkgdir/usr/src/linux-$_kernver/arch" || return 1
	if [ "$CARCH" = "x86_64" ]; then
		ln -s $KARCH x86_64 || return 1
	else
		ln -s $KARCH i386 || return 1
	fi
#	cd "$pkgdir/usr/src/linux-$_kernver/arch/x86/include" || return 1
#	ln -sf ../../../include/acpi ./acpi
	cd "$OLDPWD" || return 1

	msg "Removing unneeded architectures..."
	for i in "$pkgdir/usr/src/linux-$_kernver/arch/"*; do
		[ "${i##*/}" != "$KARCH" ] && rm -rf "$i"
	done

	msg "Fixing permissions..."
	chown -R root:root "$pkgdir/usr/src/linux-$_kernver" &&
		find "$pkgdir/usr/src/linux-$_kernver" -type d -exec chmod 755 {} \; || return 1

	# remove unneeded architectures
	rm -rf ${pkgdir}/usr/src/linux-${_kernver}/arch/{alpha,arm,arm26,avr32,blackfin,cris,frv,h8300,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,parisc,powerpc,ppc,s390,sh,sh64,sparc,sparc64,um,v850,xtensa}

	msg "Kernel source installing finished."

	if [ "${_use_git_version}" != "y" ]; then
		pkgver=$stdver
	fi
}

package_linux-api-headers() {
	pkgdesc="Kernel headers sanitized for use in userspace, from git tree."
	provides=('kernel-headers' "kernel-headers=$stdver")
	conflicts=('kernel-headers')

	cd "${_builddir}"
	_kernver="$(make kernelrelease)"

	msg "Installing the kernel headers..."
	#make mrproper || return 1
	#make headers_check || return 1
	make INSTALL_HDR_PATH="${pkgdir}/usr" headers_install || return 1
	rm -rf "${pkgdir}/usr/include/drm"
	rm -f $(find ${pkgdir} -name .install -or -name ..install.cmd)

	if [ "$_keep_build_dir" != "y" ]; then
		# Remove build directory
		rm -rf "${_builddir}"
	fi
	msg "kernel headers installed finished."

	if [ "${_use_git_version}" != "y" ]; then
		pkgver=$stdver
	fi
}

md5sums=('385a5d6d48fbc08ca88b95df577f67aa'
         '5e777ff68d871106d9bfa203f5e92750'
         'b6d05566fcfe07770d9386f0588f8758'
         'b7f8561498606f6e1d730bbd1ca699ab'
         'cebee1045a00b7632223807f799f9823'
         'f37649ebde983a9aea20ac881ed9f0e7')
